<?php
declare(strict_types=1);

namespace App\Resource\RoomPK\RoomPK;


use Carbon\Carbon;
use Illuminate\Http\Resources\Json\JsonResource;

class Resource extends JsonResource
{
    public function toArray($request)
    {
        return [
            "id" => $this->id,
            "type" => $this->type,
            "room_id" => $this->room_id,
            "match_room_id" => $this->match_room_id,
            "winner_room_id" => $this->winner_room_id,
            "score" => $this->score,
            "match_score" => $this->match_score,
            "room" => [
                "id" => $this->room->id,
                "code" => $this->room->room_id,
                "name" => $this->room->room_name,
                "cover" => $this->room->cover_picture,
                "score" => $this->score,
                "host" => !empty($this->room->holder) ? $this->room->holder: $this->room->user_id,
                "is_open_rival_mike" => $this->room_is_open_rival_mike,
                "holder_is_favorite" => $this->room->holder_is_favorite,
                "holder_user_id" => $this->room->holder_user_data->user_id ?? 0,
                "holder_user_code" => $this->room->holder_user_data->user_code ?? 0,
                'holder_head_picture' => $this->room->holder_user_data->head_picture ?? '',
            ],
            "match" => [
                "id" => $this->match_room->id,
                "code" => $this->match_room->room_id,
                "name" => $this->match_room->room_name,
                "cover" => $this->match_room->cover_picture,
                "score" => $this->match_score,
                "host" => $this->match_room->holder ?? 0,
                "is_open_rival_mike" => $this->match_room_is_open_rival_mike,
                "holder_is_favorite" => $this->match_room->holder_is_favorite,
                "holder_user_id" => $this->match_room->match_roomholder_user_data->user_id ?? 0,
                "holder_user_code" => $this->match_room->holder_user_data->user_code ?? 0,
                'holder_head_picture' => $this->match_room->holder_user_data->head_picture ?? '',
            ],
            "contributors" => $this->contributors,
            "match_contributors" => $this->matchContributors,
            "public_room_id" => $this->public_room_id,
            "status" => $this->status,
            "second" => $this->second,
            // "room_is_open_rival_mike" => $this->room_is_open_rival_mike,
            // "match_room_is_open_rival_mike" => $this->match_room_is_open_rival_mike,
            "start_at" => $this->start_at,
            "start_time" => Carbon::make($this->start_at)->timestamp,
            "end_at" => $this->end_at,
            "end_time" => Carbon::make($this->end_at)->timestamp,
            "countdown" => (time() <= Carbon::make($this->start_at)->timestamp) ? 0 : $this->second - time() - Carbon::make($this->start_at)->timestamp,
            "time" => (int)(microtime(true)*1000),
        ];
    }
}
